/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-11-22 16:19:19
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-06-04 15:51:24
 * @FilePath: \cxl-driving-training\src\views\column\list\index.ts
 */
import { BaseData, BaseMethod } from '@/utils/basc-data'
import { tableConfig } from './config'
import { reactive, ref } from 'vue'
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus'
import { useRoute } from 'vue-router'
import { dicItemtDetele, dictInformationByDictId, dictItemAdd, dictItemEdit } from '@/apis/system/dict'
import router from '@/router'
import { routerStore } from '@/store/router.store'
import { DICT_DATA } from '@/utils/dict-data'
import { isJSON } from '@/utils'

export default class DictItemData extends BaseData implements BaseMethod {
    dialog = reactive({
        visible: false,
        title: '新增字典',
        type: 'create',
        width: '420px'
    })
    dictForm = reactive({
        name: '',
        value: '',
        valType: '1', //1字符串 2 json对象
        jsonVal: '',
        desc: '',
        id: '',
        color: '',
        sortOrder: '0',
        type: '1' //字典类型 1后台管理，2小程序
    })
    dictRules = {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        value: [{ required: false, message: '请输入值', trigger: 'blur' }],
        type: [{ required: true, message: '请选择字典类型', trigger: 'change' }]
    }

    dictId = ref('')
    dictType = ref('1')
    jsonVal = ref()
    routerStore = routerStore()
    constructor() {
        super(tableConfig)
        const route = useRoute()
        this.dictId.value = route.query?.id as string
        this.dictType.value = route.query?.dictType as string
        if (Number(this.dictType.value) === 2) {
            this.dialog.width = '70%'
        }

        this.getData()
    }
    getData = (arg?: any) => {
        dictInformationByDictId(
            this.dictId.value,
            this.pageData.pagination.pageNo,
            this.pageData.pagination.pageSize
        ).then(res => {
            if (Number(res.data.code) === 200) {
                this.pageData.table.data.length = 0
                this.pageData.pagination.total = res.data.result.total

                const arr: any[] = res.data.result.records

                arr.forEach((element: any) => {
                    this.pageData.table.data.push(element)
                })
            }
        })
    }
    onSearch = (arg?: any) => {}
    onReset = (arg?: any) => {}
    onPaginationChange = (arg?: any) => {
        this.pageData.pagination.pageNo = arg
        this.getData()
    }
    onPaginationSize = (arg?: any) => {
        this.pageData.pagination.pageNo = 1
        this.pageData.pagination.pageSize = arg
        this.getData()
    }
    onOperation = (arg?: any) => {
        const { type, data } = arg
        console.log(type, data)
        if (type === 'create') {
            this.dialog.visible = true
            this.dialog.type = type
            this.dialog.title = '新增'
            this.dictForm.id = ''
            this.dictForm.name = ''
            this.dictForm.value = ''
            this.dictForm.sortOrder = '0'
            this.dictForm.color = ''
            this.dictForm.desc = ''
            this.dictForm.valType = '1'
            this.dictForm.jsonVal = ''
        } else if (type === 'edit') {
            this.dialog.visible = true
            this.dialog.type = type
            this.dialog.title = '编辑'
            this.dictForm.id = data.id
            this.dictForm.name = data.itemText

            this.dictForm.sortOrder = data.sortOrder + ''
            this.dictForm.color = data.color
            this.dictForm.desc = data.description
            if (Number(this.dictType.value) === 2) {
                this.dictForm.valType = isJSON(data.itemValue) ? '2' : '1'
                if (Number(this.dictForm.valType) === 1) {
                    this.dictForm.value = data.itemValue
                    this.dictForm.jsonVal = ''
                } else if (Number(this.dictForm.valType) === 2) {
                    this.dictForm.value = ''
                    this.dictForm.jsonVal = JSON.parse(data.itemValue)
                }
            } else {
                this.dictForm.value = data.itemValue
            }
        } else if (type === 'delete') {
            ElMessageBox.confirm('确认要删除该字典?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    dicItemtDetele(data.id).then(res => {
                        if (Number(res.data.code) === 200) {
                            this.getData()
                            DICT_DATA.getDictData(true)
                        }
                    })
                })
                .catch(() => {
                    //
                })
        } else if (type === 'switch') {
            //
        }
    }
    onGoBack = () => {
        this.routerStore.removeHeaderRoute(this.routerStore.selectIndex)
        router.push('/dict-list')
    }
    confirm = () => {
        console.log(JSON.parse(this.jsonVal.value))
    }
    //监听弹窗关闭
    onCloseDialog = (formEl: FormInstance | undefined) => {
        if (!formEl) return
        this.dialog.visible = false
        formEl.resetFields()
    }
    //监听弹窗点击确定按钮
    submitForm = async (formEl: FormInstance | undefined) => {
        if (!formEl) return
        await formEl.validate((valid, fields) => {
            if (valid) {
                const itemValue: string =
                    Number(this.dictForm.valType) === 1 ? this.dictForm.value : this.dictForm.jsonVal
                if (this.dialog.type === 'edit') {
                    dictItemEdit({
                        description: this.dictForm.desc,
                        itemText: this.dictForm.name,
                        itemValue,
                        dictId: this.dictId.value,
                        color: this.dictForm.color,
                        sortOrder: Number(this.dictForm.sortOrder),
                        id: this.dictForm.id
                    }).then(res => {
                        if (Number(res.data.code) === 200) {
                            this.dialog.visible = false
                            formEl.resetFields()
                            this.getData()
                            DICT_DATA.getDictData(true)
                        }
                    })
                } else {
                    dictItemAdd({
                        description: this.dictForm.desc,
                        itemText: this.dictForm.name,
                        itemValue,
                        dictId: this.dictId.value,
                        color: this.dictForm.color,
                        sortOrder: Number(this.dictForm.sortOrder)
                    }).then(res => {
                        if (Number(res.data.code) === 200) {
                            this.dialog.visible = false
                            formEl.resetFields()
                            this.getData()
                            DICT_DATA.getDictData(true)
                        }
                    })
                }
            } else {
                console.log('error submit!', fields)
            }
        })
    }
}
